function varargout = romer_simulation(varargin)
% ROMER_SIMULATION M-file for romer_simulation.fig
%      ROMER_SIMULATION, by itself, creates a new ROMER_SIMULATION or raises the existing
%      singleton*.
%
%      H = ROMER_SIMULATION returns the handle to a new ROMER_SIMULATION or the handle to
%      the existing singleton*.
%
%      ROMER_SIMULATION('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ROMER_SIMULATION.M with the given input arguments.
%
%      ROMER_SIMULATION('Property','Value',...) creates a new ROMER_SIMULATION or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before romer_simulation_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to romer_simulation_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help romer_simulation

% Last Modified by GUIDE v2.5 01-Jun-2011 11:38:48

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @romer_simulation_OpeningFcn, ...
                   'gui_OutputFcn',  @romer_simulation_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function print_error_msg(handles, msg)
    set(handles.compilation_text, 'String', msg, 'ForegroundColor', 'Red');

function print_success_msg(handles)
    set(handles.compilation_text, 'String', 'Operation completed successfully!', 'ForegroundColor', [0.48, 0.98, 0]); % green
    
function print_warning_msg(handles, msg)
    set(handles.compilation_text, 'String', msg, 'ForegroundColor', [1, 0.5, 0]); % orange

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes just before romer_simulation is made visible.
function romer_simulation_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to romer_simulation (see VARARGIN)

global was_topology_drawn;

% Choose default command line output for romer_simulation
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes romer_simulation wait for user response (see UIRESUME)
% uiwait(handles.figure1);

was_topology_drawn = 0;

params = ParamsSingleton.instance();
params.main_axes = handles.Axes;

xtick = get(handles.Axes, 'XTick');
ytick = get(handles.Axes, 'YTick');
set(handles.Axes, 'Color', 'Black', 'XColor', 'Green','YColor', 'Green', 'XTick', xtick, 'YTick', ytick);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Outputs from this function are returned to the command line.
function varargout = romer_simulation_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_max_haps_radius_Callback(hObject, eventdata, handles)
% hObject    handle to input_max_haps_radius (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_max_haps_radius as text
%        str2double(get(hObject,'String')) returns contents of input_max_haps_radius as a double

input = str2num(get(hObject,'String'));

%checks to see if input is empty. if so, default input1_editText to zero
if (isempty(input))
     set(hObject,'String','0')
end
guidata(hObject, handles)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_max_haps_radius_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_max_haps_radius (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_number_of_nodes_Callback(hObject, eventdata, handles)
% hObject    handle to input_number_of_nodes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_number_of_nodes as text
%        str2double(get(hObject,'String')) returns contents of input_number_of_nodes as a double

input = str2num(get(hObject,'String'));

%checks to see if input is empty. if so, default input1_editText to zero
if (isempty(input))
     set(hObject,'String','0')
end
guidata(hObject, handles)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_number_of_nodes_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_number_of_nodes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in draw_topo_button.
function draw_topo_button_Callback(hObject, eventdata, handles)
% hObject    handle to draw_topo_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global topology;
global was_topology_drawn;

number_of_nodes = str2num(get(handles.input_number_of_nodes, 'String'));
maximum_radius = str2num(get(handles.input_max_haps_radius, 'String'));
maximum_radius_to_gaps = str2num(get(handles.input_max_gaps_radius, 'String'));

if (isempty(number_of_nodes) || number_of_nodes == 0)
    print_error_msg(handles, 'Error: Insert a number!');
else
    params = ParamsSingleton.instance();
    topology = Topology(number_of_nodes, maximum_radius, maximum_radius_to_gaps, params.use_romer);
    global_depositor = GlobalPacketDepositor.instance();
    global_depositor.set_nodes(topology.nodes);
    
    TopologyDrawer.draw(topology);
    was_topology_drawn = 1;
    print_success_msg(handles);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in find_now.
function find_now_Callback(hObject, eventdata, handles)
% hObject    handle to find_now (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global topology;
global was_topology_drawn;

if (was_topology_drawn == 0)
    print_error_msg(handles, 'Error: First draw a topology');
    return
end

start_node = str2num(get(handles.input_start_node,'String'));
if (isempty(start_node) || start_node == 0)
    print_error_msg(handles, 'Error: Start node is empty!');
    return
elseif (start_node > topology.number_of_hap_nodes)
    print_error_msg(handles, 'Error: Illegal start node');
    return
end

end_node = str2num(get(handles.input_end_node,'String'));
if (isempty(end_node) || end_node == 0)
    print_error_msg(handles, 'Error: End node is empty!');
    return
    elseif (end_node > (topology.number_of_gap_nodes + topology.number_of_hap_nodes))
    print_error_msg(handles, 'Error: Illegal end node');
    return
end

[cost, path] = Simulator.find_and_draw_dijkstra_path(topology, start_node, end_node);
path_str = get_path_as_string(cost, path);

set(handles.out_min_cost, 'String', num2str(cost));
set(handles.out_shortest_path, 'String', path_str);

if (cost == Inf)
    print_warning_msg(handles, 'Nodes are not connected');
else
    print_success_msg(handles);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_start_node_Callback(hObject, eventdata, handles)
% hObject    handle to input_start_node (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_start_node as text
%        str2double(get(hObject,'String')) returns contents of input_start_node as a double

input = str2num(get(hObject,'String'));

%checks to see if input is empty. if so, default input1_editText to zero
if (isempty(input))
     set(hObject,'String','0')
end
guidata(hObject, handles)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_start_node_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_start_node (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_end_node_Callback(hObject, eventdata, handles)
% hObject    handle to input_end_node (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_end_node as text
%        str2double(get(hObject,'String')) returns contents of input_end_node as a double

input = str2num(get(hObject,'String'));

%checks to see if input is empty. if so, default input1_editText to zero
if (isempty(input))
     set(hObject,'String','0')
end
guidata(hObject, handles)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_end_node_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_end_node (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in find_romer_path_button.
function find_romer_path_button_Callback(hObject, eventdata, handles)
% hObject    handle to find_romer_path_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global topology;
global was_topology_drawn;

if (was_topology_drawn == 0)
    print_error_msg(handles, 'Error: First draw a topology');
    return
end

start_node_id = str2num(get(handles.input_start_node,'String'));
if (isempty(start_node_id) || start_node_id == 0)
    print_error_msg(handles, 'Error: Start node is empty!');
    return
elseif (start_node_id > topology.number_of_hap_nodes)
    print_error_msg(handles, 'Error: Illegal start node');
    return
end

end_node_id = str2num(get(handles.input_end_node,'String'));
if (isempty(end_node_id) || end_node_id == 0)
    print_error_msg(handles, 'Error: End node is empty!');
    return
elseif (end_node_id > topology.number_of_gap_nodes + topology.number_of_hap_nodes)
    print_error_msg(handles, 'Error: Illegal end node');
    return
end

print_warning_msg(handles, 'Running...');
SingletonInitializer.create_all_singletons();
Simulator.find_and_draw_romer_paths(topology, start_node_id, end_node_id, true);
%Simulator.simulate_file_download_packet_generator(topology, start_node_id, end_node_id);
%Simulator.simulate_web_browsing_packet_generator(topology, start_node_id, end_node_id);
%Simulator.simulate_voip_packet_generator(topology, start_node_id, end_node_id);

%if (cost == Inf)
%    print_warning_msg(handles, 'Nodes are not connected');
%else
print_success_msg(handles);
%end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function path_str = get_path_as_string(cost, path)

path_str = '';
if (cost ~= Inf)
    path_str = num2str(path(1));
    if (size(path, 2) > 1)
        for i = 2:(size(path, 2))
            path_str = [path_str, ' --> ', num2str(path(i))];
        end
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in save_topology_button.
function save_topology_button_Callback(hObject, eventdata, handles)
% hObject    handle to save_topology_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global topology;
global was_topology_drawn;

if (was_topology_drawn == 0)
    print_error_msg(handles, 'Error: First draw a topology');
    return
end

[filename,pathname] = uiputfile('topology.mat','Save your topology');
if pathname == 0 %if the user pressed cancelled, then we exit this callback
    return
end
topology.save_topology_to_file(pathname, filename);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in load_topology_button.
function load_topology_button_Callback(hObject, eventdata, handles)
% hObject    handle to load_topology_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global topology;
global was_topology_drawn;

[filename, pathname] = uigetfile('*.mat', 'Choose the topology file to load');
if pathname == 0 %if the user pressed cancelled, then we exit this callback
    return
end

params = ParamsSingleton.instance();
router_type = params.router_type_ROMER;
topology = Topology.read_topology_from_file(fullfile(pathname, filename), router_type);
TopologyDrawer.draw(topology);
was_topology_drawn = 1;
set(handles.input_number_of_nodes,'String',num2str(topology.number_of_hap_nodes))
print_success_msg(handles);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_max_gaps_radius_Callback(hObject, eventdata, handles)
% hObject    handle to input_max_gaps_radius (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_max_gaps_radius as text
%        str2double(get(hObject,'String')) returns contents of input_max_gaps_radius as a double

input = str2num(get(hObject,'String'));

%checks to see if input is empty. if so, default input1_editText to zero
if (isempty(input))
     set(hObject,'String','0')
end
guidata(hObject, handles)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_max_gaps_radius_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_max_gaps_radius (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in simulation_button.
function simulation_button_Callback(hObject, eventdata, handles)
% hObject    handle to simulation_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global topology;
global was_topology_drawn;

if (was_topology_drawn == 0)
    print_error_msg(handles, 'Error: First draw a topology');
    return
end

print_warning_msg(handles, 'Running...');
Simulator.simulate_multiple_connections(topology);
print_success_msg(handles);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_romer_alpha_Callback(hObject, eventdata, handles)
% hObject    handle to input_romer_alpha (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_romer_alpha as text
%        str2double(get(hObject,'String')) returns contents of input_romer_alpha as a double

romer_alpha = str2num(get(handles.input_romer_alpha,'String'));
if (isempty(romer_alpha) || romer_alpha == 0)
    print_error_msg(handles, 'Error: Romer alpha is empty!');
    return
end

params = ParamsSingleton.instance();
params.romer_alpha = romer_alpha;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_romer_alpha_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_romer_alpha (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_link_loss_rate_Callback(hObject, eventdata, handles)
% hObject    handle to input_link_loss_rate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_link_loss_rate as text
%        str2double(get(hObject,'String')) returns contents of input_link_loss_rate as a double

loss_rate = str2num(get(handles.input_link_loss_rate,'String'));
if (isempty(loss_rate) || loss_rate == 0)
    print_error_msg(handles, 'Error: link loss rate is empty!');
    return
end

params = ParamsSingleton.instance();
params.link_loss_rate_in_precentage = loss_rate;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_link_loss_rate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_link_loss_rate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_node_buffer_size_mb_Callback(hObject, eventdata, handles)
% hObject    handle to input_node_buffer_size_mb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_node_buffer_size_mb as text
%        str2double(get(hObject,'String')) returns contents of input_node_buffer_size_mb as a double

buffer_size = str2num(get(handles.input_node_buffer_size_mb,'String'));
if (isempty(buffer_size) || buffer_size == 0)
    print_error_msg(handles, 'Error: Node buffer size is empty!');
    return
end

params = ParamsSingleton.instance();
params.adapter_max_queue_size_in_bytes = buffer_size;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_node_buffer_size_mb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_node_buffer_size_mb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function edit14_Callback(hObject, eventdata, handles)
% hObject    handle to edit14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit14 as text
%        str2double(get(hObject,'String')) returns contents of edit14 as a double

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function edit14_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_file_download_packet_size_Callback(hObject, eventdata, handles)
% hObject    handle to input_file_download_packet_size (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_file_download_packet_size as text
%        str2double(get(hObject,'String')) returns contents of input_file_download_packet_size as a double

input_file_download_packet_size = str2num(get(handles.input_file_download_packet_size,'String'));
if (isempty(input_file_download_packet_size) || input_file_download_packet_size == 0)
    print_error_msg(handles, 'Error: File download packet size is empty!');
    return
end

params = ParamsSingleton.instance();
params.file_download_packet_size_in_bytes = input_file_download_packet_size;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_file_download_packet_size_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_file_download_packet_size (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_web_browing_packet_size_Callback(hObject, eventdata, handles)
% hObject    handle to input_web_browing_packet_size (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_web_browing_packet_size as text
%        str2double(get(hObject,'String')) returns contents of input_web_browing_packet_size as a double

input_web_browing_packet_size = str2num(get(handles.input_web_browing_packet_size,'String'));
if (isempty(input_web_browing_packet_size) || input_web_browing_packet_size == 0)
    print_error_msg(handles, 'Error: Web browsing packet size is empty!');
    return
end

params = ParamsSingleton.instance();
params.web_browsing_packet_size_in_bytes = input_web_browing_packet_size;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_web_browing_packet_size_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_web_browing_packet_size (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_voip_packet_size_Callback(hObject, eventdata, handles)
% hObject    handle to input_voip_packet_size (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_voip_packet_size as text
%        str2double(get(hObject,'String')) returns contents of input_voip_packet_size as a double

input_voip_packet_size = str2num(get(handles.input_voip_packet_size,'String'));
if (isempty(input_voip_packet_size) || input_voip_packet_size == 0)
    print_error_msg(handles, 'Error: VOIP packet size is empty!');
    return
end

params = ParamsSingleton.instance();
params.voip_packet_size_in_bytes = input_voip_packet_size;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_voip_packet_size_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_voip_packet_size (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_file_download_data_rate_Callback(hObject, eventdata, handles)
% hObject    handle to input_file_download_data_rate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_file_download_data_rate as text
%        str2double(get(hObject,'String')) returns contents of input_file_download_data_rate as a double

input_file_download_data_rate = str2num(get(handles.input_file_download_data_rate,'String'));
if (isempty(input_file_download_data_rate) || input_file_download_data_rate == 0)
    print_error_msg(handles, 'Error: File download data rate is empty!');
    return
end

params = ParamsSingleton.instance();
params.file_download_data_rate_in_Kbps = input_file_download_data_rate;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_file_download_data_rate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_file_download_data_rate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_web_browsing_data_rate_Callback(hObject, eventdata, handles)
% hObject    handle to input_web_browsing_data_rate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_web_browsing_data_rate as text
%        str2double(get(hObject,'String')) returns contents of input_web_browsing_data_rate as a double

input_web_browsing_data_rate = str2num(get(handles.input_web_browsing_data_rate,'String'));
if (isempty(input_web_browsing_data_rate) || input_web_browsing_data_rate == 0)
    print_error_msg(handles, 'Error: Web browsing data rate is empty!');
    return
end

params = ParamsSingleton.instance();
params.web_browsing_data_rate_in_Kbps = input_web_browsing_data_rate;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_web_browsing_data_rate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_web_browsing_data_rate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_voip_data_rate_Callback(hObject, eventdata, handles)
% hObject    handle to input_voip_data_rate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_voip_data_rate as text
%        str2double(get(hObject,'String')) returns contents of input_voip_data_rate as a double

input_voip_data_rate = str2num(get(handles.input_voip_data_rate,'String'));
if (isempty(input_voip_data_rate) || input_voip_data_rate == 0)
    print_error_msg(handles, 'Error: VOIP data rate is empty!');
    return
end

params = ParamsSingleton.instance();
params.voip_data_rate_in_Kbps = input_voip_data_rate;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_voip_data_rate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_voip_data_rate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_file_download_precentage_Callback(hObject, eventdata, handles)
% hObject    handle to input_file_download_precentage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_file_download_precentage as text
%        str2double(get(hObject,'String')) returns contents of input_file_download_precentage as a double

input_file_download_precentage = str2num(get(handles.input_file_download_precentage,'String'));
if (isempty(input_file_download_precentage) || input_file_download_precentage == 0)
    print_error_msg(handles, 'Error: File download precentage is empty!');
    return
end

params = ParamsSingleton.instance();
params.file_download_precentage = input_file_download_precentage;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_file_download_precentage_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_file_download_precentage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_web_browsing_precentage_Callback(hObject, eventdata, handles)
% hObject    handle to input_web_browsing_precentage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_web_browsing_precentage as text
%        str2double(get(hObject,'String')) returns contents of input_web_browsing_precentage as a double

input_web_browsing_precentage = str2num(get(handles.input_web_browsing_precentage,'String'));
if (isempty(input_web_browsing_precentage) || input_web_browsing_precentage == 0)
    print_error_msg(handles, 'Error: Web browsing precentage is empty!');
    return
end

params = ParamsSingleton.instance();
params.web_browsing_precentage = input_web_browsing_precentage;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_web_browsing_precentage_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_web_browsing_precentage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function input_voip_precentage_Callback(hObject, eventdata, handles)
% hObject    handle to input_voip_precentage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of input_voip_precentage as text
%        str2double(get(hObject,'String')) returns contents of input_voip_precentage as a double

input_voip_precentage = str2num(get(handles.input_voip_precentage,'String'));
if (isempty(input_voip_precentage) || input_voip_precentage == 0)
    print_error_msg(handles, 'Error: VOIP precentage size is empty!');
    return
end

params = ParamsSingleton.instance();
params.voip_precentage = input_voip_precentage;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function input_voip_precentage_CreateFcn(hObject, eventdata, handles)
% hObject    handle to input_voip_precentage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in open_log_file_button.
function open_log_file_button_Callback(hObject, eventdata, handles)
% hObject    handle to open_log_file_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

params = ParamsSingleton.instance();
cmd = ['notepad.exe ' params.output_file];
dos(cmd);
